﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Elsa.Extensions;
using Elsa.Workflows;
using Elsa.Workflows.Activities;
using Elsa.Workflows.Attributes;
using Elsa.Workflows.Models;

namespace T.FAS.Runtime.MF.Activities
{
    public class FasEndActivity : End
    {
        // 输入属性：要设置的变量字典
        [Input]
        public List<EndOutputVariable> VariablesToSetWorkflowOutput { get; set; } = default!;

        protected override async ValueTask ExecuteAsync(ActivityExecutionContext context)
        {
            // 此activity执行完成后，要给Workflow的Output赋值
            foreach (var varItem in VariablesToSetWorkflowOutput)
            {

                if (varItem.SourceVarType == "string")
                {
                    var varValue = context.GetVariable<string>(varItem.SourceVariable);

                    context.WorkflowExecutionContext.Output[varItem.VarName] = varValue!;
                }
                else
                {
                    var varValue = context.GetVariable<object>(varItem.SourceVariable);

                    context.WorkflowExecutionContext.Output[varItem.VarName] = varValue!;
                }
            }
        }
    }

    public class EndOutputVariable
    {
        public string VarName { get; set; }
        public string SourceNode { get; set; }
        public string SourceVariable { get; set; }
        public string Description { get; set; }
        public string SourceVarType { get; set; }
        public string ActivityID { get; set; }
    }
}
